<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	public function __construct(){
		parent::__construct();
		
		$this->load->library('my_functions');
		$this->load->model('m_user');
				
	}
	
	public function index()
	{
			$data['name']=$this->config->item('admin');

		 	//the topnav
            $nav_data['menu'] = "home";
            $data['topnav'] = $this->load->view('backend/admin/v_nav_admin', $nav_data, TRUE);
			
		$data['content']=$this->load->view('backend/admin/v_home_admin','',TRUE);
		
		$this->load->view('backend/v_main',$data);
	}
	
	public function room()
	{
		
				// start top nav
	            $nav_data['menu'] = "room";
	            $data['topnav'] = $this->load->view('backend/admin/v_nav_admin', $nav_data, TRUE);
				// end top nav -----------------------------------------------------
				$data_room['rooms']="";
				$data['content']=$this->load->view('backend/admin/v_rooms_view',$data_room,TRUE);
				
			$this->load->view('backend/v_main',$data);
	}
	
	//view all the staff
	public function staff()
	{
		// start top nav
	            $nav_data['menu'] = "staff";
	            $data['topnav'] = $this->load->view('backend/admin/v_nav_admin', $nav_data, TRUE);
				// end top nav -----------------------------------------------------
				
				$data_staff['staff']=$this->m_user->getAllStaff();
				$data['content']=$this->load->view('backend/admin/v_staff_view',$data_staff,TRUE);
				
			$this->load->view('backend/v_main',$data);
	}
	
	//view all the access staff
	public function staff_access()
	{
		// start top nav
	            $nav_data['menu'] = "staff";
	            $data['topnav'] = $this->load->view('backend/admin/v_nav_admin', $nav_data, TRUE);
				// end top nav -----------------------------------------------------
				
				$data_staff['staff']=$this->m_user->getAllStaffAcess();
				$data['content']=$this->load->view('backend/admin/v_staff_access_view',$data_staff,TRUE);
				
			$this->load->view('backend/v_main',$data);
	}
	
	//display add staff form
	public function staff_add_form()
	{
		// start top nav
	            $nav_data['menu'] = "staff";
	            $data['topnav'] = $this->load->view('backend/admin/v_nav_admin', $nav_data, TRUE);
				// end top nav -----------------------------------------------------
				
				$data_staf['role']=$this->m_user->getAllRole();
				$data_staf['department']=$this->m_user->getAllDepartment();
				$data['content']=$this->load->view('backend/admin/v_staff_add',$data_staf,TRUE);
				
			$this->load->view('backend/v_main',$data);
	}
	
	//display add staff  access form
	public function staff_access_add_form()
	{
		// start top nav
	            $nav_data['menu'] = "staff";
	            $data['topnav'] = $this->load->view('backend/admin/v_nav_admin', $nav_data, TRUE);
				// end top nav -----------------------------------------------------
				
				$data_staf['role']=$this->m_user->getAllRole();
				$data_staf['staff']=$this->m_user->getAllStaffHasNoAcess();
				$data['content']=$this->load->view('backend/admin/v_staff_access_add',$data_staf,TRUE);
				
			$this->load->view('backend/v_main',$data);
	}
	
	//add staff process
	public function staff_add(){

		$department=$this->input->post('department');
	
		$id_card=$this->input->post('id_card');
		$name=$this->input->post('name');
		$gender=$this->input->post('gender');
		$address=$this->input->post('address');
		$email=$this->input->post('email');
		$dob=date('Y-m-d',strtotime($this->input->post('dob')));
		$phone=$this->input->post('phone');
	
		
		//save to database
		 if($this->m_user->addStaff($phone,$department,$id_card,$name,$gender,$address,$email,$dob))
		{
			$this->session->set_flashdata('sucess_notif','Staff Telah Berhasil Ditambah');
		}
		else
		{
				$this->session->set_flashdata('fail_notif','Terdapat Error Dalam Proses Penambahan Staff');
		}
	
		redirect(base_url().'admin/staff');
		
	}
	
	//add staff process
	public function staff_access_add(){

	$username=$this->input->post('username');
	$password=sha1($this->input->post('password'));
	$role=$this->input->post('role');
	$staff_id=$this->input->post('staff_name');
	$latest_login_id=$this->my_functions->getLatestLoginId();
	
		
		//save to database
		if($this->m_user->addStaffAccess($username,$password,$role) && $this->m_user->updateStaffLoginId($staff_id,$latest_login_id))
		{
			$this->session->set_flashdata('sucess_notif','Akses Staff Telah Berhasil Ditambah');
		}
		else
		{
				$this->session->set_flashdata('fail_notif','Terdapat Error Dalam Proses Penambahan Akses Staff');
		}
	
		redirect(base_url().'admin/staff_access');
		
	}
	
	//edit staff view 
	public function edit_user_form()
	{
		$staff_id=$this->uri->segment(3);
		
		// start top nav
	            $nav_data['menu'] = "staff";
	            $data['topnav'] = $this->load->view('backend/admin/v_nav_admin', $nav_data, TRUE);
				// end top nav -----------------------------------------------------
				
				$data_staf['department']=$this->m_user->getAllDepartment();
				$data_staf['staff']=$this->m_user->getStaffByName($staff_id);
				$data['content']=$this->load->view('backend/admin/v_staff_edit',$data_staf,TRUE);
				
			$this->load->view('backend/v_main',$data);
		
	}
	
	//edit the staff
	public function staff_edit()
	{
		$staff_id=$this->uri->segment(3);	
		$department=$this->input->post('department');
	
		$id_card=$this->input->post('id_card');
		$name=$this->input->post('name');
		$gender=$this->input->post('gender');
		$address=$this->input->post('address');
		$email=$this->input->post('email');
		$dob=date('Y-m-d',strtotime($this->input->post('dob')));
		$phone=$this->input->post('phone');
	
	
		
		//save to database
		 if($this->m_user->editStaff($staff_id,$phone,$department,$id_card,$name,$gender,$address,$email,$dob))
		{
			$this->session->set_flashdata('sucess_notif','Staff Telah Berhasil Diubah');
		}
		else
		{
				$this->session->set_flashdata('fail_notif','Terdapat Error Dalam Proses Pengubahan Staff');
		}
	
		redirect(base_url().'admin/staff');
	}
	
	//edit staff  access view 
	public function edit_access_form()
	{
		$login_id=$this->uri->segment(3);
		
		// start top nav
	            $nav_data['menu'] = "staff";
	            $data['topnav'] = $this->load->view('backend/admin/v_nav_admin', $nav_data, TRUE);
				// end top nav -----------------------------------------------------
				$data_staf['role']=$this->m_user->getAllRole();
				$data_staf['department']=$this->m_user->getAllDepartment();
				$data_staf['staff']=$this->m_user->getStaffLoginByName($login_id);
				$data_staf['staffs']=$this->m_user->getAllStaffHasNoAcess();
				$data['content']=$this->load->view('backend/admin/v_staff_access_edit',$data_staf,TRUE);
				
			$this->load->view('backend/v_main',$data);
		
	}
	
	
	//edit the staff
	public function staff_access_edit()
	{
		$login_id=$this->uri->segment(3);	
		$username=$this->input->post('username');
		$password=sha1($this->input->post('password'));
		$role_id=$this->input->post('role');
		$staff_id=$this->input->post('staff_name');
				
		
		//save to database
		if($this->m_user->editStaffAccess($login_id,$username,$password,$role_id) && $this->m_user->updateStaffLoginId($staff_id,$login_id))
		{
			$this->session->set_flashdata('sucess_notif','Akses Staff Telah Berhasil Diubah');
		}
		else
		{
				$this->session->set_flashdata('fail_notif','Terdapat Error Dalam Proses Pengubahan Akses Staff');
		}
	
		redirect(base_url().'admin/staff_access');
	}
	

	public function staff_delete()
	{
		$staff_id=$this->uri->segment(3);
		$login_id=$this->my_functions->getStaffLoginId($staff_id);
		
		if($this->m_user->deleteStaff($staff_id,$login_id) && $this->m_user->deleteLoginStaff($login_id))
		{
			$this->session->set_flashdata('sucess_notif','Akses Staff Telah Berhasil Dihapus');
		}
		else
		{
				$this->session->set_flashdata('fail_notif','Terdapat Error Dalam Proses Penghapusan Staff');
		}
	
		redirect(base_url().'admin/staff_access');
	}
	
	
	
}
?>